<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 9-28/0028
 * Time: 15:33
 * y.12301.cc订单修改
 */
$ordernum  = trim($_REQUEST['ordernum']);
$tid       = intval($_REQUEST['tid']);//array('tid'=>'num')
$tnum      = intval($_REQUEST['tnum']);
if (!$ordernum) {
    exit('{"code":106,"msg":"订单号不能为空"}');
}
if (!$tid) {
    exit('{"code":106,"msg":"票ID不能为空"}');
}
if (!$tnum) {
    exit('{"code":106,"msg":"票数不能小于1"}');
}
//$salerid      = intval($_REQUEST['tnum']);

$soap      = SoapInit::GetSoapInside();
//查找出订单
$orderXml = $soap->Order_Globle_Search("","","","","","","","","","","","",
    $ordernum,"","","","","",1,1,0,10,"","","","","","","","","",
    $concat_id,$ifpack);
$orderArr = simplexml_load_string($orderXml);
//TODO::套票子票禁止修改
if($orderArr->Rec->UUifpack==2) {
    exit('{"code":106,"msg":"套票子票禁止修改"}');
}
//TODO::验证订单所有者
$formt = $orderArr->Rec->UUfromt;

$mem     = new \pft\Member\MemberAccount($GLOBALS['le']);

if ($_SESSION['dtype']==0) {
    $apply_did = $_SESSION['memberID'];
}
elseif ($_SESSION['dtype']==2) {
    //TODO:查找出资源方的上级供应商
    $account = $mem->getAccountById($_SESSION['memberID']);
    $sql = "SELECT apply_did FROM uu_land WHERE salerid=$account LIMIT 1";
    $GLOBALS['le']->query($sql);
    $rows = $GLOBALS['le']->fetch_assoc();
    $apply_did = $GLOBALS['le']->f('apply_did');
}
$SellId = $orderArr->Rec->UUsellid;
//$account = $mem->getAccountById($apply_did);
if ($apply_did != $SellId) {
    exit('{"code":106,"msg":"订单所属关系错误，无权限修改"}');
}

if($tid!=(int)$orderArr->Rec->UUtid) {
    exit('{"code":106,"msg":"票类不对应，无法修改'.$orderArr->Rec->UUtid.'"}');
}
if($orderArr->Rec->UUstatus!=0) {
    $statusList =array(
        "0"=>"未使用",
        "1"=>"已使用",
        "2"=>"已过期",
        "3"=>"已取消",
        "4"=>"凭证码被替代",
        "6"=>"被终端撤销(已取消)",
        "5"=>"被终端修改----(已使用)",
    );
    exit('{"code":106,"msg":"订单'.$statusList[(int)$orderArr->Rec->UUstatus].'，无法修改"}');
}
if ($tnum==0) {
    exit('{"code":106,"msg":"不能将订单门票数改为0，请通过取消操作"}');
}
if ($tnum == (int)$orderArr->Rec->UUtnum) {
    exit('{"code":106,"msg":"票数没有改变"}');
}
elseif ( $tnum > (int)$orderArr->Rec->UUtnum) {
    exit('{"code":106,"msg":"不能增加订单门票数"}');
}
$totalprice=(float)$orderArr->Rec->UUtprice * $tnum;
$res = $soap->Order_Change_Pro($ordernum, $tnum, $totalprice, 0, $_SESSION['memberID']);
$dir = ROOTPATH . 'd/m/logs/';
$file = $dir . 'terminal_order_modify_'.date('m').'.log';
if (!is_dir($dir)) {
    mkdir($dir, 0777);
    chmod($dir, 0777);
}
$log = "|修改订单票数|用户ID:{$_SESSION['memberID']},票ID:{$tid},票数:{$tnum}";
if ($res==100) {
    write_logs( 'OK'.$log, $file);
    exit('{"code":200,"msg":"修改成功"}');
}
else {
    write_logs('FAIL'.$log, $file);
    exit('{"code":106,"msg":"修改失败,错误代码'.$res.'"}');
}